﻿using System;
using System.Linq;
using System.Text;

namespace WinForm.Helper
{
    public static class UtilStr
    {
        /// <summary>
        /// 替换中文名字的除最后一个名字外为星号
        /// </summary>
        /// <param name="s">要替换的字符串</param>
        /// <returns>字符串的长度</returns>
        public static string StrNameReplace(string s)
        {
            StringBuilder value = new StringBuilder();
            try
            {
                if (!string.IsNullOrEmpty(s))
                {
                    s = s.Replace("\0", "");
                    var sl = s.ToList();
                    int length = s.Length;
                    if (length == 2)
                    {
                        //string lastS = sl[length - 1].ToString();
                        //value.Append("*");
                        //value.Append(lastS);
                        string firstS = sl[0].ToString();
                        value.Append(firstS);
                        value.Append("*");
                    }
                    else if (length >= 3)
                    {
                        string lastS = sl[length - 1].ToString();
                        string firstS = sl[0].ToString();
                        value.Append(firstS);
                        string star = GetStar(length - 2);
                        value.Append(star);
                        value.Append(lastS);
                    }
                }
            }
            catch (Exception)
            {

            }
            return value.ToString();
        }

        /// <summary>
        /// 造星星
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetStar(int length)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                if (length > 0)
                {
                    for (int i = 0; i < length; i++)
                    {
                        sb.Append("*");
                    }
                }
            }
            catch (Exception)
            {

            }
            return sb.ToString();
        }

        /// <summary>
        /// 身份证后四位为数字，其他为星号
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string StrIDCardReplace(string s)
        {
            StringBuilder value = new StringBuilder();
            try
            {
                if (!string.IsNullOrEmpty(s))
                {
                    s = s.Replace("\0", "");
                    var sl = s.ToList();
                    int length = s.Length;

                    if (length == 15)
                    {
                        //value.Append(GetStar(11));
                        //for (int i = 11; i < 15; i++)
                        //{
                        //    value.Append(sl[i]);
                        //}
                        for (int i = 0; i < 11; i++)
                        {
                            value.Append(sl[i]);
                        }
                        value.Append(GetStar(4));

                    }
                    else if (length == 18)
                    {
                        //value.Append(GetStar(14));
                        //for (int i = 14; i < 18; i++)
                        //{
                        //    value.Append(sl[i]);
                        //}
                        for (int i = 0; i < 14; i++)
                        {
                            value.Append(sl[i]);
                        }
                        value.Append(GetStar(4));
                    }
                }
            }
            catch (Exception)
            {

            }
            return value.ToString();
        }
    }
}
